﻿Imports MySoft.Map.Data
Imports MySoft.Map.Caching

Partial Public Class SqlScheduleSetting
    Inherits Mysoft.Map.Application.Security.AppPage
    Private Shared jobName As String = MySoft.Map.LMS.Initialization.GetSqlJobName()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Content.Text = "<script language=""javascript""> var __xml=""" & GetScheduleSetting() & """;</script>"
        End If
    End Sub


    '取数据库中调度计划设置信息
    '返回XML格式的字符串
    Private Function GetScheduleSetting() As String
        Dim sql As String
        Dim xml As String = ""
        Dim sqlVersion = MySoft.Map.LMS.MyDBExtend.GetDBVersion()
        Dim dt As DataTable

        If sqlVersion = "2000" Then
            sql = "SELECT jobsch.enabled ,jobsch.active_start_time,jobsch.active_start_date,jobsch.active_end_date ,"
            sql += "case jobsch.active_end_date when '99991231' then 1 else 0 end  AS OverType  "
            sql += "FROM msdb.dbo.sysjobsteps job INNER JOIN msdb.dbo.sysjobschedules jobsch ON job.job_id=jobsch.job_id "
            sql += " WHERE job.job_id = (select job_id from msdb.dbo.sysjobs where [name]='" + jobName + "')"
        Else
            sql = "SELECT sch.enabled ,sch.active_start_time,sch.active_start_date,sch.active_end_date ,"
            sql += "case sch.active_end_date when '99991231' then 1 else 0 end  AS OverType  "
            sql += " FROM msdb.dbo.sysjobsteps job INNER JOIN msdb.dbo.sysjobschedules jobsch ON job.job_id=jobsch.job_id "
            sql += "INNER JOIN msdb.dbo.sysschedules  sch ON jobsch.schedule_id=sch.schedule_id "
            sql += "WHERE job.job_id = (select job_id from msdb.dbo.sysjobs where [name]='" + jobName + "')"
        End If

        dt = MySoft.Map.LMS.MyDBExtend.GetDataTable(sql)
        If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
            xml = "<schedule>"
            xml += "<enabled>" & dt.Rows(0)("enabled").ToString() & "</enabled>"
            xml += "<active_start_time>" & dt.Rows(0)("active_start_time").ToString() & "</active_start_time>"
            xml += "<active_start_date>" & dt.Rows(0)("active_start_date").ToString() & "</active_start_date>"
            xml += "<active_end_date>" & dt.Rows(0)("active_end_date").ToString() & "</active_end_date>"
            xml += "<overtype>" & dt.Rows(0)("overtype").ToString() & "</overtype>"
            xml += "</schedule>"
        End If
        Return xml

    End Function

    Protected Overrides Sub ConfigureForm()
        appForm.Execute()
    End Sub
End Class